home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 November / macformat-018.iso / Utility Spectacular / Developer / macgzip_022-src / macos / Posix / ThinkCPosix Sources / stat.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-26  |  2.7 KB  |  136 lines  |  [TEXT/MPS ]

  1. /* $Id: $ */
  2.  
  3. /*
  4.  * Minimal 'stat' emulation: tells directories from files and
  5.  * gives length and mtime.
  6.  *
  7.  * Largely based on code written by Guido van Rossum, CWI, Amsterdam
  8.  * and placed by him in the public domain --
  9.  * retrieved by anonymous FTP from ftp.cwi.nl
  10.  */
  11. /*
  12.  * Original from ThinkCPosix by Timothy Murphy <tim@maths.tcd.ie>,
  13.  * Trinity College Dublin
  14.  *
  15.  * Modified by SPDsoft <macspd@ivo.cps.unizar.es> to use Unix dates
  16.  * seconds from Mac 'Fri Jan  1 00:00:00 1904' to
  17.  * Un*x 'Thu Jan  1 00:00:00 1970':                    2082844800
  18.  */
  19.  
  20. #define    U2MSEC    2082844800
  21.  
  22. #include "ThinkCPosix.h"
  23.  
  24. int sys_nerr = 0;
  25. char *sys_errlist[] = {""};
  26. char *myenviron[] = {NULL};
  27. char **environ = myenviron;
  28. extern int __uid, __gid;
  29. int Stat(char*, long, struct stat*);
  30.  
  31.  
  32. /* Bits in ioFlAttrib: */
  33. #define LOCKBIT    (1<<0)        /* File locked */
  34. #define DIRBIT    (1<<4)        /* It's a directory */
  35.  
  36. /*
  37.  * Mac-ky "stat" in which filename is given relative to a directory,
  38.  * specified by long DirID.
  39.  */
  40.  
  41. int
  42. Stat(name, DirID, buf)
  43.     char *name;
  44.     long DirID;
  45.     struct stat *buf;
  46. {
  47.     CInfoPBRec cipbr;
  48.     HFileInfo *fpb = (HFileInfo*)&cipbr;
  49.     DirInfo *dpb = (DirInfo*)&cipbr;
  50.     unsigned char pname[256];
  51.     short err;
  52.  
  53.     strcpy((char*)pname, name);
  54.     c2pstr(pname);
  55.  
  56.     dpb->ioDrDirID = DirID;
  57.     fpb->ioNamePtr = pname;
  58.     fpb->ioVRefNum = 0;
  59.     fpb->ioFDirIndex = 0;
  60.     fpb->ioFVersNum = 0;
  61.     err = PBGetCatInfo(&cipbr, FALSE);
  62.     if (err != noErr) {
  63.         errno = ENOENT;
  64.         return -1;
  65.     }
  66.     if (fpb->ioFlAttrib & LOCKBIT)
  67.         buf->st_mode= 0444;
  68.     else
  69.         buf->st_mode= 0666;
  70.     if (fpb->ioFlAttrib & DIRBIT) {
  71.         buf->st_mode |= 0111 | S_IFDIR;
  72.         buf->st_size= dpb->ioDrNmFls;
  73.         buf->st_rsize= 0;
  74.     }
  75.     else {
  76.         buf->st_mode |= S_IFREG;
  77.         if (fpb->ioFlFndrInfo.fdType == 'APPL')
  78.             buf->st_mode |= 0111;
  79.         buf->st_size= fpb->ioFlLgLen;
  80.         buf->st_rsize= fpb->ioFlRLgLen;
  81.     }
  82.     buf->st_atime= fpb->ioFlCrDat - U2MSEC;
  83.     buf->st_mtime= fpb->ioFlMdDat - U2MSEC;
  84.     buf->st_ctime= fpb->ioFlCrDat - U2MSEC;
  85.     buf->st_ino= (unsigned short)fpb->ioDirID;
  86.     buf->st_uid= __uid;
  87.     buf->st_gid= __gid;
  88.     buf->st_FlFndrInfo= fpb->ioFlFndrInfo;
  89.     return 0;
  90. }
  91.  
  92. int
  93. stat(path, buf)
  94.     char *path;
  95.     struct stat *buf;
  96. {
  97.     return Stat(path, 0L, buf);
  98. }
  99.  
  100. int
  101. fstat(fd, buf)
  102.     int fd;
  103.     struct stat *buf;
  104. {
  105.     FCBPBRec fcb;
  106.     unsigned char pname[256];
  107.     long DirID;
  108.     short err;
  109.     
  110.     /*
  111.      * fdopen() gives FILE entry with name of file,
  112.      * as well as RefNum of containing directory
  113.      */
  114.     
  115.     FILE *fp = fdopen(fd, "");
  116.     
  117.     /* 
  118.      * PBGetFCBInfo() converts short RefNum to long DirID
  119.      */
  120.      
  121.     fcb.ioRefNum= fp->refnum;
  122.     fcb.ioVRefNum= 0;
  123.     fcb.ioFCBIndx= 0;
  124.     fcb.ioNamePtr= pname;
  125.     err= PBGetFCBInfo(&fcb, FALSE);
  126.     if (err != noErr) {
  127.         errno = ENOENT;
  128.         return -1;
  129.     }
  130.     DirID = fcb.ioFCBParID;
  131.     
  132.     p2cstr(pname);
  133.     return Stat((char*)pname, DirID, buf);
  134. }
  135.  
  136.